package com.hotel.tv.application.admin.services.auth.impl;

import com.autumn.mybatis.wrapper.EntityQueryWrapper;
import com.autumn.zero.authorization.application.dto.users.UserAddInput;
import com.autumn.zero.authorization.application.dto.users.UserDetailsOutput;
import com.autumn.zero.authorization.application.dto.users.UserInput;
import com.autumn.zero.authorization.application.dto.users.UserOutput;
import com.autumn.zero.authorization.application.services.impl.AbstractUserAppService;
import com.hotel.tv.application.admin.services.auth.UserAppService;
import com.hotel.tv.entities.auth.HotelRole;
import com.hotel.tv.entities.auth.HotelUser;
import com.hotel.tv.repositories.auth.HotelUserRepository;
import com.hotel.tv.runtime.session.HotelAdminSession;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

/**
 * <p>
 * </p>
 *
 * @description TODO
 * @author: 老码农
 * @create: 2020-04-10 12:21
 **/
@Service
@Primary
public class UserAppServiceImpl
        extends AbstractUserAppService<HotelUser, HotelRole, HotelUserRepository, UserAddInput, UserInput, UserOutput, UserDetailsOutput>
        implements UserAppService {

    public UserAppServiceImpl() {
        super(HotelUser.class, UserOutput.class, UserDetailsOutput.class);
    }

    @Override
    protected void systemByCriteria(EntityQueryWrapper<HotelUser> wrapper) {
        HotelAdminSession session = (HotelAdminSession) this.getSession();
        if (session.isAdministratorUser()) {
            wrapper.lambda().where().eq(HotelUser::getUserType, session.getUserType());
        } else {
            wrapper.lambda().where().eq(HotelUser::getUserType, session.getUserType())
                    .eq(HotelUser::getOrganizeId, session.getOrganizeId());
        }
    }

    @Override
    protected void setUserDefault(HotelUser entity) {
        super.setUserDefault(entity);
        HotelAdminSession session = (HotelAdminSession) this.getSession();
        entity.setUserType(session.getUserType());
        entity.setOrganizeId(session.getOrganizeId());
    }
}
